package com.dzzw.Dao;

import com.dzzw.Model.Vote;
import com.dzzw.utils.DButils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

//投票主题
public class VoteDao {
    private final Connection con = DButils.getCon();
    //新增投票主题
    public void VoteAdd(Vote vote){
        String sql = "insert into t_vote(vote_title,vote_introduce) value(?,?)";
        try{
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setString(1, vote.getVote_title());
            preparedStatement.setString(2, vote.getVote_introduce());
            preparedStatement.executeUpdate();
            preparedStatement.close();
            con.close();
        } catch(Exception e) {
            System.out.println("数据添加失败！");

            e.printStackTrace();
        }
    }

    //修改投票主题
    public void VoteModify(Vote vote){
        String sql = "update t_vote set vote_title=?,vote_introduce=? where vote_id=?";
        try{
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setString(1,vote.getVote_title());
            preparedStatement.setString(2,vote.getVote_introduce());
            preparedStatement.setDate(3,vote.getVote_startime());
            preparedStatement.executeUpdate();
            preparedStatement.close();
            con.close();
        }catch (Exception e){
            System.out.println("数据修改失败！");

            e.printStackTrace();
        }
    }

    //删除投票主题
    public void VoteDelete(int vote_id) {
        String sql = "Delete from t_vote where vote_id = ?";
        try {
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setInt(1,vote_id);
            preparedStatement.executeUpdate();
            preparedStatement.close();
            con.close();
        }catch(Exception e){
            System.out.println("数据单条删除失败！");

            e.printStackTrace();
        }
    }

    //查看全部投票主题
    public static List<Vote> SelectAllVote(){
        List<Vote> list = new ArrayList<>();
        String sql = "SELECT * from t_vote";
        try{
            Connection con =DButils.getCon();
            PreparedStatement preparedStatement =con.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                Vote vote = new Vote();
                vote.setVote_id(resultSet.getInt(1));
                vote.setVote_title(resultSet.getString(2));
                vote.setVote_introduce(resultSet.getString(3));
                vote.setVote_startime(resultSet.getDate(4));
                vote.setVote_endtime(resultSet.getDate(5));
                list.add(vote);
            }
            resultSet.close();
            preparedStatement.close();
            con.close();
            System.out.println("查询所有数据成功！");
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }
}
